<?php

class Sending extends AppModel
{

	public $name = 'Sending';

	public $displayField = 'send_date';

	protected $types = array(
	   'OFFER_ON_TESTING',
	   'COMPLETED_TESTING',
	   'REPORT',
	);

	public $validate = array(
	);

	public $belongsTo = array(
        'Test' => array('className' => 'Test'),
        'Testing' => array('className' => 'Testing'),
        'Report' => array('className' => 'Report'),
        'Reporting' => array('className' => 'Reporting'),
        'Candidate' => array('className' => 'Candidate'),
        'ClientAdmin' => array('className' => 'ClientAdmin'),
        'Client' => array('className' => 'Client'),
        'AuthorAdmin' => array('className' => 'Admin',
            'foreignKey' => 'author_admin_id',
        ),
        'AuthorClientAdmin' => array('className' => 'ClientAdmin',
            'foreignKey' => 'author_client_admin_id',
        ),
	);

	function generateList($conds = array())
	{
	    $this->rel();
        return parent::generateList($conds, $this->primaryKey, null, "{n}.{$this->name}.{$this->primaryKey}", "{n}.{$this->name}.{$this->displayField}");
	}

	function allowDelete($fields = null)
	{
        return true;
	}

	function allowUpdate($fields = null)
	{
        return false;
	}

	function sended(MailModel $model, $params = array())
	{
        $data = array();
        if (isset($params['mode'])) {
            switch ($params['mode']) {

            case 'offer_on_testing':
                $data['type']            = 'OFFER_ON_TESTING';
                $data['test_id']         = @$params['test_id'];
                $data['testing_id']      = @$params['testing_id'];
                $data['candidate_id']    = @$params['candidate_id'];
                $data['client_admin_id'] = @$params['client_admin_id'];
                $data['client_id']       = @$params['client_id'];
                $data['author_admin_id'] = @$params['author_admin_id'];
                $data['author_client_admin_id'] = @$params['author_client_admin_id'];
                break;

            case 'end_testing':
                $data['type']            = 'COMPLETED_TESTING';
                $data['test_id']         = @$params['test_id'];
                $data['testing_id']      = @$params['testing_id'];
                $data['candidate_id']    = @$params['candidate_id'];
                $data['client_admin_id'] = @$params['client_admin_id'];
                $data['client_id']       = @$params['client_id'];
                $data['author_admin_id'] = @$params['author_admin_id'];
                $data['author_client_admin_id'] = @$params['author_client_admin_id'];
                break;

            case 'generate_report':
                $data['type']            = 'REPORT';
                $data['report_id']       = @$params['report_id'];
                $data['reporting_id']    = @$params['reporting_id'];
                $data['test_id']         = @$params['test_id'];
                $data['testing_id']      = @$params['testing_id'];
                $data['candidate_id']    = @$params['candidate_id'];
                $data['client_admin_id'] = @$params['client_admin_id'];
                $data['client_id']       = @$params['client_id'];
                $data['author_admin_id'] = @$params['author_admin_id'];
                $data['author_client_admin_id'] = @$params['author_client_admin_id'];
                break;
            }
        }
        if (!empty($data)) {
            $data['mail_from'] = $model->from;
            $data['rcpt_to']   = $model->to;
            $data['subject']   = $model->subject;
            $data['content']   = $model->message;
            $data['send_date'] = @$params['send_date'];

            $this->save($this->create($data));
        }
	}

	function getTypes()
	{
	    return $this->types;
	}

	function getTypesList()
	{
	    $result = array();
	    foreach ($this->types as $type) {
	        $result[$type] = __d('sendings', 'type_'.$type, true);
	    }
	    return $result;
	}
}

?>